﻿using System.IO;
using JQuery.WebKit.Common.Caching;

namespace JQuery.WebKit.Common.Utilities
{
    public static class CachedFile
    {
        public static string GetFileContent(string path)
        {
            string item = (string)Cache.Current.GetItem(path);
            if (item == null)
            {
                RefreshFileCache(path);
                item = (string)Cache.Current.GetItem(path);
            }
            return item;
        }

        private static object _lock1 = new object();
        private static void RefreshFileCache(string path)
        {
            lock (_lock1)
            {
                Cache.Current.RemoveItem(path);
                if (File.Exists(path))
                {
                    string content = File.ReadAllText(path);
                    Cache.Current.AddItem(path, content);
                }
            }
        }
    }
}
